package http;

import okhttp3.*;
import java.io.IOException;

/**
 * 如果需要使用http的API添加用户，就必须先开启用户名密码认证方式，否则该使用该API会出现Not found
 */
public class EmqxTest {
    public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

    public static void main(String[] args) {
        testUserName();
        //testConnections();
        //testMqtt_subscribe();
    }


    /**
     *@Author: caoxiabo on 2019/12/18 8:52
     *@Description: 用户名验证
     */
    private static void testUserName(){
        String url = "http://106.52.3.122:8081/api/v4/auth_username";
        //System.out.println("resp:"+doGet(url));//查询用户名
        //resp:{"code":0,"data":["paper_server_0003","paper_0004","paper_0001","paper_server_0002","paper_0003","paper_0002","paper_server_0004","paper_server_0001"]}

        /*String json = "{\"username\":\"rwzn88\",\"password\":\"123456\"}";
        System.out.println("resp:"+doPost(url,json));//添加用户
        //resp:{"code":0}*/

/*        String url_delete = "http://106.52.3.122:8080/api/v3/auth_username/paper_server_0004";
        System.out.println("resp:"+doDelete(url_delete));//删除用户*/

/*        String url_update = "http://106.52.3.122:8080/api/v3/auth_username/paper_server_0002";
        String json = "{\"password\":\"888888\"}";
        System.out.println("resp:"+doPut(url_update,json));//更新用户*/

        /*String find_update = "http://106.52.3.122:8080/api/v3/auth_username/paper_server_0002";
        System.out.println("resp:"+doGet(find_update));//查具体用户*/

    }

    /**
     *@Author: caoxiabo on 2019/12/18 13:32
     *@Description: 连接api 验证测试
     */
    private static void testConnections(){
        /*String url = "http://106.52.3.122:8081/api/v4/connections";
        System.out.println("resp:"+doGet(url));//查询连接集合*/

/*        String url = "http://106.52.3.122:8080/api/v3/connection/username/paper_server_0001";
        System.out.println("resp:"+doGet(url));//查询某个用户下的连接集合*/

/*        String url = "http://106.52.3.122:8080/api/v3/connections/s_paper_0004";
        System.out.println("resp:"+doDelete(url));//剔除某客户端连接*/

        /*String url = "http://106.52.3.122:8080/api/v3//nodes/emqx@127.0.0.1/connection/username/paper_server_0001";
        System.out.println("resp:"+doGet(url));//查询某个节点下某个用户下的连接集合*/
    }

    /**
     *@Author: caoxiabo on 2019/12/18 13:49
     *@Description: 订阅发布api测试
     */
    private static void testMqtt_subscribe(){
       /* String url = "http://106.52.3.122:8081/api/v3/mqtt/subscribe";
        String json = "{\"client_id\": \"s_paper_0003\",\"qos\": 0,\"topic\": \"test11\"}";
        System.out.println("resp:"+doPost(url,json));//订阅主题，该客户端必须先连接才能订阅*/

/*        String url = "http://106.52.3.122:8080/api/v3//mqtt/unsubscribe";
        String json = "{\"client_id\": \"s_paper_0003\",\"qos\": 0,\"topic\": \"test\"}";
        System.out.println("resp:"+doPost(url,json));//取消订阅，连接断开了，目前订阅自动取消，但该api的执行，还是要连接*/

/*        String url = "http://106.52.3.122:8080/api/v3/subscriptions";
        System.out.println("resp:"+doGet(url));//所有主题订阅*/

/*        String url = "http://106.52.3.122:8080/api/v3/nodes/emqx@127.0.0.1/subscriptions/";
        System.out.println("resp:"+doGet(url));//单个节点的主题订阅*/

/*        String url = "http://106.52.3.122:8080/api/v3/subscriptions/s_paper_0003";
        System.out.println("resp:"+doGet(url));//单个客户端的主题订阅*/

        String url = "http://106.52.3.122:8081/api/v4/mqtt/publish";
        String json = "{\"client_id\":\"c_paper_0001\",\"qos\":0,\"topic\":\"test\",\"payload\":\"Hello, World!\"}";
        System.out.println("resp:"+doPost(url,json));//给主题发布消息
    }

    /**
     *@Author: caoxiabo on 2019/12/18 9:37
     *@Description: post 测试验证
     */
    private static void testPost(){
        String url = "http://106.52.3.122:8080/api/v3/auth_username";
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, "{\"username\":\"paper_0002\",\"password\":\"123456\"}");
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization",Utils.BASE64_EMQX_AUTHORIZATION)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            System.out.println("resp:"+response.body().string());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:38
     *@Description: get api
     */
    private static String doGet(String url){
        //创建一个Request
        final Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization",Utils.BASE64_EMQX_AUTHORIZATION)
                .build();
        return  execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:37
     *@Description: post api
     */
    private static String doPost(String url,String json){
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization",Utils.BASE64_EMQX_AUTHORIZATION)
                .post(body)
                .build();
        return execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:36
     *@Description: put api
     */
    private static String doPut(String url,String json){
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization",Utils.BASE64_EMQX_AUTHORIZATION)
                .put(body)
                .build();
        return execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 10:11
     *@Description: 没有请求参数删除
     */
    private static String doDelete(String url){
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization",Utils.BASE64_EMQX_AUTHORIZATION)
                .delete()
                .build();
        return execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:35
     *@Description: 删除 api
     */
    private static String doDelete(String url,String json){
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization",Utils.BASE64_EMQX_AUTHORIZATION)
                .delete(body)
                .build();
        return execute(request);
    }

    /**
     *@Author: caoxiabo on 2019/12/18 8:22
     *@Description: 执行请求
     */
    private static String execute(Request request){
        try {
            OkHttpClient client = new OkHttpClient();
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "{\"error\":\"fail\"}";
    }
}
